﻿Sharpener
==========

Sharpener is a simple optimizer for CLR assemblies, written in C#. 

Its purpose in life is to help squeeze that little, last bit of performance out of your code, for those who really need it.

Disclaimer
----------

You use this software at your own risk!

License
-------

Sharpener is free software, licensed under the terms of the MIT License. 

Credits
-------

| Sharpener, LineCommander - Timothy A. Barela
| Mono.Cecil - Jb Evain.

Usage
-----

``sharpener [options] inputfilename``

Where options are:

--stripcallvirt  Replaces callvirt instructions with call, where possible.
--forceinline  Inline expansion of methods marked with the Sharpener.ForceInlineAttribute.
--watermark  Adds a Sharpener.SharpenedAttribute to the output assembly.
--all  Adds all above passes. If nothing is specified, this is the default.
--out=filename  Sets the filename for the output assembly. Defaults to <SourceAssembly>Optimized.exe|dll.
--quiet  Shuts sharpener up.

By referencing the sharpener.exe assembly in your own project, you can mark methods with the **Sharpener.ForceInlineAttribute**. Calls to the marked method, will now be replaced(expanded) with the method's code. After running Sharpener, the marked methods and sharpener.exe reference will be removed from the output assembly.

Optimizations
-------------

As of version 11.11.0.0, sharpener can apply the following optimizations, which *may* improve runtime performance.

Replace callvirts
_________________

Sharpener removes non virtual callvirt instructions inserted by the C# and VB compilers. These are put in so that the JIT will test for null object references at call sites. By removing these checks, you get somewhere in the neighborhood of 1% perf gain. YMMV.

Be warned that calling methods on null instances will now work, as long as no instance fields or properties are invoked. This has the potential for subtle bugs...

Method inlining
_______________

By marking your methods with the **Sharpener.ForceInlineAttribute**, calls to the method will be removed, and the method body itself will be patched in at the call site.

Method inlining is preliminary. It currently has the following limitations:

* The method must be static. 
* The method should not call itself. ( No check is done for this yet, expect problems with this! ) 
* The method may not be generic. 
* Local variables and parameters are added as new local variable slots in the caller. Currently, No analysis is done to optimize or share these slots. This results in some bloated and redundant IL. I would like to improve this over time, but for now its a starting point. 
* Not tested very well. 

Note that smaller code size is a good thing. Inlining recklessly will probably hurt you. Generally the JIT should be left to its own devices. This feature is really for the times where you have specific places in your code, and you know it will give you a boost. 

Future Plans
------------

* More thorough testing. 
* Allow for inlining of instance methods. 
* Improve the code quality of inlined method callsites. 
* Automatic(?) inlining of valuetype operator overloads. 
* Possibly a few other obscure or language specific optimizations. 
* Possibly some performance analysis/profiling functionality.

Code, Bugs and Feature Requests
-------------------------------

Sharpener is hosted at https://bitbucket.org/tbarela/sharpener

You can grab the sourcecode, report bugs, or make suggestions there.  

Build with VisualStudio 2008 or SharpDevelop 3+.
